#pragma once

#ifndef MAINWINDOW_H_
#define MAINWINDOW_H_

#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QtCore/QSettings>
#include <QtCore/QStack>
#include <QtGui/QMdiSubWindow>
#include <QTGui/QToolButton>
#include <QtGui/QCommandLinkButton>

#include "ui_MainWindow.h"
#include "AppSettings.h"
#include "ConnectionDialog.h"
#include "Tab.h"
#include "TableViewGadget.h"

#include "ConnectionTreeModel.h"
#include "DBObject.h"
#include "MSSQLConnection.h"
#include "MySQLConnection.h"

namespace SQLDoc
{
	class MainWindow : public QMainWindow, public Ui::MainWindow
	{
		Q_OBJECT

	public:
		MainWindow(QWidget* parent = 0, Qt::WFlags flags = 0);
		virtual ~MainWindow();

	private:		
		QSqlError connectToSqlServer(const QString &driver, const QString &sqlEngine, const QString &hostname, const QString &username, const QString &password, const int &port, QString &conName);
		void addConnectionToTree(const QString &driver, const QString &sqlEngine, const QString &hostname, const QString &username, const QString &password, const int &port, const QString &conName);
		QMenu* createConnectionContextMenu(void);
		QMenu* createDatabaseContextMenu(void);
		QMenu* createTableContextMenu(void);
		void refreshStartPage(void);
		void addLastConnection(const QString &driver, const QString &sqlEngine, const QString &hostname, const QString &username, const QString &password, const int &port);
		void saveLastConnectionList(void);
		void loadLastConnectionList(void);

		QSqlDatabase m_db;
		DBObject *m_root;
		QMenu *m_connectionMenu;
		QMenu *m_databaseMenu;
		QMenu *m_tableMenu;
		ConnectionTreeModel *m_connectionTreeModel;
		QList<QString> m_lastConnections;

	public slots:
		/* Top menu events */
		void addConnection_triggered(void);
		void connectionsVisible_triggered(void);
		void startPageVisible_triggered(void);		

		/* Context menu events */
		void closeConnection_triggered(void);
		void refreshConnection_triggered(void);
		void tableDetails_triggered(void);
		void generateDocumentation_triggered(void);
		void refreshDatabase_triggered(void);

		/* Other events */
		void connectionsTreeView_clicked(const QModelIndex& index);
		void connectionsTreeView_doubleClicked(const QModelIndex& index);
		void window_tabsChanged(int count);
		void exportDoc_clicked(void);
		void showContextMenu(const QPoint &pos);
		void recentConnectionButtonClicked(void);
	};
}
#endif
